﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using Gift.Utile;
using System.Web.Security;

namespace WebSite.cn
{
    public partial class login : BasePage
    {

        public string sLoginMessage  = "";

        // Methods
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["msg"]))
                {
                    sLoginMessage = "<font color='red' size='3'>提示：请先登录后，到礼品商城选取您要兑换的商品！</font>";
                }
            }
        }

        protected void butSubmit_Click(object sender, EventArgs e)
        {

            string strMessage = string.Empty;
            CheckSubmit(txtName.Text, txtPass.Text);
        }

        protected void CheckSubmit(string username, string password)
        {
            string strMessage = string.Empty;
            if (username.Length == 0)
            {
                strMessage += " - 用户名不能为空!";
            }
            else
            {
                if (username.Length < 4 || username.Length > 20)
                {
                    strMessage += " - 用户名(4-20)位数字与字符!";
                }
            }
            if (password.Length == 0)
            {
                strMessage += " - 密　码不能为空!";
            }
            else
            {
                if (password.Length < 4 || password.Length > 20)
                {
                    strMessage += " - 密　码(4-20)位数字与字符!";
                }
            }
            if (!string.IsNullOrEmpty(strMessage))
            {
                Message.Msg(this.Page, strMessage, MessageType.Failure);
                return;
            }

            Gift.DAL.DALMember theMember = new Gift.DAL.DALMember();
            Gift.Model.DB_Member oMember = theMember.GetModel(username, FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5"));
            LoginMember = oMember;
            if (oMember == null)
            {
                Message.Msg(this.Page, "用户密码错误!", MessageType.Failure);
            }
            else if (oMember.MemberId == 0)
            {
                Message.Msg(this.Page, "用户名不存在!", MessageType.Failure); 
            }
            else
            {
                Message.Msg(this.Page, "登录成功!", MessageType.Success);
               // System.Threading.Thread.Sleep(3000);
               //// Response.Redirect("index.aspx");
                HttpContext.Current.Response.Write(Gift.Utile.Utile.WrapJavaScript(
                                  "setTimeout('window.parent.location.reload()',2000)"));

                                    //" parent.isenternum=1;" +
                                    //" parent.document.getElementById('floatBoxBg').style.display='none';" +
                                    //" parent.document.getElementById('floatBox').style.display='none';" +
                                    //" parent.document.getElementById('MemberFromLogin').style.display='none'; " +
                                    //" parent.document.getElementById('MemberFromOnline').style.display='block'; " +
                                    //" parent.document.getElementById('lbName').innerHTML ='" + username + "';"));
            }

           
        }

   

    }
}